package com.hengx.cellview.cell;

import android.graphics.Paint;
import android.graphics.Canvas;
import android.view.MotionEvent;
import com.hengx.cellview.CellView;
import java.util.ArrayList;
import java.util.List;

public class CellGroup extends Cell {
    
    private List<Cell> cells;
    
    public CellGroup(Cell... items) {
        this.cells = new ArrayList<>();
        if (items != null) {
            for(Cell c : items) {
                add(c); // 使用添加单元格的方法
            }
        }
    }
    
    public void add(Cell cell) {
        if (cell != null && !cells.contains(cell)) {
            cells.add(cell);
        }
    }
    
    public void add(int index, Cell cell) {
    	if (cell != null && !cells.contains(cell)) {
            cells.add(index, cell);
        }
    }
    
    public Cell get(int index) {
    	return cells.get(index);
    }
    
    public boolean remove(Cell cell) {
        return cells.remove(cell);
    }
    
    public void remove(int index) {
    	cells.remove(index);
    }
    
    public void clear() {
        cells.clear();
    }
    
    public int getCount() {
        return cells.size();
    }
    
    public int indexOf(Cell cell) {
        return cells.indexOf(cell);
    }
    
    public boolean contains(Cell cell) {
        return cells.contains(cell);
    }
    
    @Override
    public void draw(Paint paint, Canvas canvas, float scaleFactor) {
        for(Cell c : cells) {
            c.draw(paint, canvas, scaleFactor);
        }
    }
    
    @Override
    public void setBounds(float x, float y, float endX, float endY) {
        super.setBounds(x, y, endX, endY);
        for(Cell c : cells) {
            c.setBounds(x, y, endX, endY);
        }
    }
    
    @Override
    public boolean onTouch(CellView view, MotionEvent event) {
        boolean ret = super.onTouch(view, event);
        for(Cell c : cells) {
            ret |= c.onTouch(view, event);
        }
        return ret;
    }
    
    @Override
    public void init(CellView view) {
        super.init(view);
        for(Cell c : cells) {
            c.init(view);
        }
    }
    
    @Override
    public void onSizeChanged(int width, int height) {
        super.onSizeChanged(width, height);
        for(Cell c : cells) {
            c.onSizeChanged(width, height);
        }
    }
    
    
    
}